package yxy.game.pm2.module.hero;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

public class HeroModule extends PlayerModule {
	// bean===========================================================================
	private int heroPackUpper; // 武将背包上限
	private int heroPackUpperBuyTimes;// 武将背包上限购买次数
	private DateTime lastTime; // 刷新时间
	private int rebirthTimes;// 武将重生次数

	public int getHeroPackUpper() {
		return heroPackUpper;
	}

	public void setHeroPackUpper(int heroPackUpper) {
		this.heroPackUpper = heroPackUpper;
	}

	public int getHeroPackUpperBuyTimes() {
		return heroPackUpperBuyTimes;
	}

	public void setHeroPackUpperBuyTimes(int heroPackUpperBuyTimes) {
		this.heroPackUpperBuyTimes = heroPackUpperBuyTimes;
	}

	public DateTime getLastTime() {
		return lastTime;
	}

	public void setLastTime(DateTime lastTime) {
		this.lastTime = lastTime;
	}

	public int getRebirthTimes() {
		return rebirthTimes;
	}

	public void setRebirthTimes(int rebirthTimes) {
		this.rebirthTimes = rebirthTimes;
	}

	// func===========================================================================

	/** 刷新每日的状态 */
	private void update() {
		try {
			if (tryLock()) {
				DateTime now = DateTime.now();
				if (getLastTime() == null || !new LocalDate(now).equals(new LocalDate(getLastTime()))) {
					setLastTime(now);
					setRebirthTimes(0);
					this.set();
				}
			}
		} catch (Exception e) {
		} finally {
			unlock();
		}
	}

	// redis==========================================================================

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:HEROINFO", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public static HeroModule get(String playerCode) {
		try {
			HeroModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), HeroModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static HeroModule create(String playerCode) {
		HeroModule module = new HeroModule();
		module.setPlayerCode(playerCode);
		module.setHeroPackUpper(fomula.武将背包上限初始.intValue());
		module.update();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				HeroModule heroModule = getv(key(playerCode));
				MySQLCacheDAO.setV(key, heroModule);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
